package com.ruoyi.applet.service.impl;

import java.util.List;

import com.ruoyi.applet.OutDTO.AppletVideoDTO;
import com.brisk.common.utils.DateUtils;
import com.brisk.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.applet.mapper.AppletVideoMapper;
import com.ruoyi.applet.domain.AppletVideo;
import com.ruoyi.applet.service.IAppletVideoService;
import org.springframework.util.ObjectUtils;

/**
 * 视频Service业务层处理
 *
 * @author PFD
 * @date 2022-08-18
 */
@Service
public class AppletVideoServiceImpl implements IAppletVideoService {
    @Autowired
    private AppletVideoMapper appletVideoMapper;

    /**
     * 查询视频
     *
     * @param videoId 视频主键
     * @return 视频
     */
    @Override
    public AppletVideo selectAppletVideoByVideoId(Long videoId) {
        return appletVideoMapper.selectAppletVideoByVideoId(videoId);
    }

    /**
     * 查询视频列表
     *
     * @param appletVideo 视频
     * @return 视频
     */
    @Override
    public List<AppletVideo> selectAppletVideoList(AppletVideo appletVideo) {
        return appletVideoMapper.selectAppletVideoList(appletVideo);
    }

    /**
     * 新增视频
     *
     * @param appletVideo 视频
     * @return 结果
     */
    @Override
    public int insertAppletVideo(AppletVideo appletVideo) {
        //查询是否有
        AppletVideo appletVideo1 = appletVideoMapper.getVideoBySiteId(appletVideo.getSiteId());
        if (ObjectUtils.isEmpty(appletVideo1)) {
            appletVideo.setCreateTime(DateUtils.getNowDate());
            appletVideo.setCreateBy(SecurityUtils.getUsername());
            return appletVideoMapper.insertAppletVideo(appletVideo);
        }
        return -1;
    }

    /**
     * 修改视频
     *
     * @param appletVideo 视频
     * @return 结果
     */
    @Override
    public int updateAppletVideo(AppletVideo appletVideo) {
        try {
            return appletVideoMapper.updateAppletVideo(appletVideo);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 批量删除视频
     *
     * @param videoIds 需要删除的视频主键
     * @return 结果
     */
    @Override
    public int deleteAppletVideoByVideoIds(Long[] videoIds) {
        return appletVideoMapper.deleteAppletVideoByVideoIds(videoIds);
    }

    /**
     * 删除视频信息
     *
     * @param videoId 视频主键
     * @return 结果
     */
    @Override
    public int deleteAppletVideoByVideoId(Long videoId) {
        return appletVideoMapper.deleteAppletVideoByVideoId(videoId);
    }

    @Override
    public AppletVideoDTO getAppletSiteVideoBySiteId(Long siteId) {
        AppletVideo appletVideo = appletVideoMapper.getVideoBySiteId(siteId);
        if (ObjectUtils.isEmpty(appletVideo)) {
            return null;
        }
        AppletVideoDTO appletVideoDTO = new AppletVideoDTO();
        appletVideoDTO.setVideoUrl(appletVideo.getVideoUrl());
        appletVideoDTO.setVideoId(appletVideo.getVideoId());
        appletVideoDTO.setVideoName(appletVideo.getVideoName());
        appletVideoDTO.setSiteName(appletVideo.getSiteName());
        appletVideoDTO.setSiteId(appletVideo.getSiteId());
        appletVideoDTO.setEndImg(appletVideo.getEndImg());
        return appletVideoDTO;
    }
}
